package com.fanfan.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fanfan.dao.RoleDao;
import com.fanfan.pojo.ArticleTag;
import com.fanfan.pojo.Role;
import com.fanfan.pojo.RoleMenu;
import com.fanfan.pojo.User;
import com.fanfan.pojo.domain.ResponseResult;
import com.fanfan.pojo.vo.PageVo;
import com.fanfan.pojo.vo.RoleDto;
import com.fanfan.pojo.vo.RoleVo;
import com.fanfan.pojo.vo.UserVo;
import com.fanfan.service.RoleMenuService;
import com.fanfan.service.RoleService;
import com.fanfan.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author fansea
 * @since 2023-02-24 14:05:35
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
    @Autowired
    RoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        if (id==1L){
            List<String> list=new ArrayList<>();
            list.add("admin");
            return list;
        }
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult listRole(Integer pageNum, Integer pageSize, Role role) {
        LambdaQueryWrapper<Role> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.hasText(role.getRoleName()),Role::getRoleName,role.getRoleName());
        queryWrapper.eq(StringUtils.hasText(role.getStatus()),Role::getStatus,role.getStatus());
        queryWrapper.orderByAsc(Role::getRoleSort);
        Page<Role> page=new Page();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);
        List<RoleVo> roleVos = BeanCopyUtils.copyList(page.getRecords(), RoleVo.class);
        PageVo pageVo=new PageVo(roleVos,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public void addRole(RoleDto roleDto) {
        /*List<ArticleTag> articleTags = articleDto.getTags().stream()
                .map(tagId -> new ArticleTag(article.getId(), tagId))
                .collect(Collectors.toList());*/

        Role role = BeanCopyUtils.copyOne(roleDto, Role.class);
        save(role);
        System.out.println("role的id为"+role.getId());
        List<RoleMenu> collect = roleDto.getMenuIds().stream()
                .map(menuId -> new RoleMenu(role.getId(), menuId))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(collect);
    }

    @Override
    public ResponseResult showRole(Long id) {
        return ResponseResult.okResult(getBaseMapper().selectById(id));
    }

    @Override
    public ResponseResult updateRole(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyOne(roleDto, Role.class);
        getBaseMapper().updateById(role);
        List<RoleMenu> roleMenus = roleDto.getMenuIds().stream()
                .map(menuId -> new RoleMenu(roleDto.getId(), menuId))
                .collect(Collectors.toList());
        /*roleMenus.stream()
                .map(roleMenu -> getBaseMapper().updateIt(roleMenu));*/
//        roleMenuService.updateBatchById(roleMenus);
        LambdaQueryWrapper<RoleMenu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId,role.getId());
        roleMenuService.getBaseMapper().delete(queryWrapper);
        roleMenuService.saveBatch(roleMenus);
        return ResponseResult.okResult();

    }

}












